/*
 * Class Commands
 *
 * Version 1.0
 *
 * Moday, August 18, 2008
 *
 * Created by Palidino76
 */

package palidino76.rs2.io.packets;

import java.io.BufferedWriter;
import java.io.FileWriter;
import palidino76.rs2.Server;
import palidino76.rs2.Engine;
import palidino76.rs2.players.Player;
import palidino76.rs2.util.Misc;

public class Commands implements Packet {
    /**
     * Handles commands, chat text that starts with ::.
     * @param p The Player which the frame should be handled for.
     * @param packetId The packet id this belongs to.
     * @param packetSize The amount of bytes being recieved for this packet.
     */
    public void handlePacket(Player p, int packetId, int packetSize) {
        if (p == null || p.stream == null) {
            return;
        }
        try {
            String playerCommand = p.stream.readString().toLowerCase();
            String[] cmd = playerCommand.split(" ");
            if (p.rights >= 0) {
                if (cmd[0].equals("male")) {
                    p.look[0] = 7; // Hair
                    p.look[1] = 16; // Beard
                    p.look[2] = 18; // Torso
                    p.look[3] = 28; // Arms
                    p.look[4] = 34; // Bracelets
                    p.look[5] = 38; // Legs
                    p.look[6] = 42; // Shoes
                    p.gender = 0;
                    p.appearanceUpdateReq = true;
                    p.updateReq = true;
                } else if (cmd[0].equals("female")) {
                    p.look[0] = 48; // Hair
                    p.look[1] = 1000; // Beard
                    p.look[2] = 57; // Torso
                    p.look[3] = 64; // Arms
                    p.look[4] = 68; // Bracelets
                    p.look[5] = 77; // Legs
                    p.look[6] = 80; // Shoes
                    p.gender = 1;
                    p.appearanceUpdateReq = true;
                    p.updateReq = true;
		    } else if(cmd[0].equals("godwarsdungeon")) {
                    p.teleportTo(2882, 5311, 2, 4, 0, 8939, 8941, 1576, 0, 1577, 0);
		    } else if(cmd[0].equals("saradominaltar")) {
                    p.teleportTo(2907, 5265, 0, 4, 0, 8939, 8941, 1576, 0, 1577, 0);
		    } else if(cmd[0].equals("zamorakaltar")) {
                    p.teleportTo(2925, 5331, 2, 4, 0, 8939, 8941, 1576, 0, 1577, 0);
		    } else if(cmd[0].equals("bandosaltar")) {
                    p.teleportTo(2864, 5354, 2, 4, 0, 8939, 8941, 1576, 0, 1577, 0);
		    } else if(cmd[0].equals("armadylaltar")) {
                    p.teleportTo(2839, 5296, 2, 4, 0, 8939, 8941, 1576, 0, 1577, 0);
                } else if (cmd[0].equals("bank")) {
                    p.openBank();
		    } else if(cmd[0].equals("players")) {
			  p.frames.sendMessage(p, "There is currently " + Engine.getPlayerCount() + " player(s) online.");
                }
            }
            if (p.rights >= 1) {
            }
            if (p.rights >= 2) {
                if (cmd[0].equals("showinterface")) {
                    p.frames.showInterface(p, Integer.parseInt(cmd[1]));
		    } else if(cmd[0].equals("emote")) {
			  p.requestAnim(Integer.parseInt(cmd[1]), 0);
		    } else if(cmd[0].equals("gfx")) {
			  p.requestGFX(Integer.parseInt(cmd[1]), 0);
                } else if (cmd[0].equals("npc")) {
                    Server.engine.newNPC(Integer.parseInt(cmd[1]), p.absX, p.absY, p.heightLevel, p.absX + 1, p.absY + 1, p.absX + -1, p.absY + -1, false);
                } else if (cmd[0].equals("tele")) {
                    int x = Integer.parseInt(cmd[1]);
                    int y = Integer.parseInt(cmd[2]);
                    int h = Integer.parseInt(cmd[3]);
                    p.teleportTo(x, y, h, 4, 0, 8939, 8941, 1576, 0, 1577, 0);
                } else if (cmd[0].equals("restorestats")) {
                    for (int i1 = 0; i1 < p.skillLvl.length; i1++) {
                    p.skillLvl[i1] = p.getLevelForXP(i1);
        	    	  p.frames.setSkillLvl(p, i1);
		        }
                } else if (cmd[0].equals("restoreenergy")) {
                    p.runEnergy = 100;
            	  p.runEnergyUpdateReq = true;
                } else if (cmd[0].equals("restorespecial")) {
                    p.specialAmount = 100;
            	  p.specialAmountUpdateReq = true;
                } else if (cmd[0].equals("emptyspecial")) {
                    p.specialAmount = 0;
            	  p.specialAmountUpdateReq = true;
                } else if(cmd[0].equals("addxp")) {
                    p.addSkillXP((double)Integer.parseInt(cmd[2]), Integer.parseInt(cmd[1]));
                } else if (cmd[0].equals("item")) {
                    Engine.playerItems.addItem(p, Integer.parseInt(cmd[1]), Integer.parseInt(cmd[2]));
                } else if (cmd[0].equals("coords")) {
                    p.frames.sendMessage(p, "x: " + p.absX + ", y: " + p.absY);
                }
            }
        } catch (Exception error) {
            error.printStackTrace();
        }
    }
}
